layui.use(['table', 'treetable'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var treeTable = layui.treetable;

    // 渲染表格
    treeTable.render({
        treeColIndex: 1,
        treeSpid: -1,
        treeIdName: 'id',
        treePidName: 'parentId',
        elem: '#munu-table',
        url: ctx + '/module/list',
        toolbar: "#toolbarDemo",
        treeDefaultClose: true,
        page: true,
        cols: [[
            {type: 'numbers'},
            {field: 'moduleName', minWidth: 100, title: '菜单名称'},
            {field: 'optValue', title: '权限码'},
            {field: 'url', title: '菜单url'},
            {field: 'createDate', title: '创建时间'},
            {field: 'updateDate', title: '更新时间'},
            {
                field: 'grade', width: 80, align: 'center', templet: function (d) {
                    if (d.grade == 0) {
                        return '<span class="layui-badge layui-bg-blue">目录</span>';
                    }
                    if (d.grade == 1) {
                        return '<span class="layui-badge-rim">菜单</span>';
                    }
                    if (d.grade == 2) {
                        return '<span class="layui-badge layui-bg-gray">按钮</span>';
                    }
                }, title: '类型'
            },
            {templet: '#auth-state', width: 180, align: 'center', title: '操作'}
        ]],
        done: function () {
            layer.closeAll('loading');
        }
    });

    /**
     * 监听头部工具栏
     */
    table.on('toolbar(munu-table)', function (data) {

        if (data.event == "expand") {
            //全部展开
            treeTable.expandAll("#munu-table");

        } else if (data.event == "fold") {
            //全部折叠
            treeTable.foldAll("#munu-table");

        } else if (data.event == "add") {
            openAddModuleDialog(0, -1);
        }
    });
    /**
     * 行工具栏
     */
    table.on('tool(munu-table)', function (data) {
        if (data.event == "add") {
            //判断当前的层级是三级，就不能添加子级
            if (data.data.grade == 2) {
                layer.msg("暂不支持四级菜单", {icon: 5})
                return;
            }
            openAddModuleDialog(data.data.grade + 1, data.data.id);
        } else if (data.event == "edit") {
            openUpdateModuleDialog(data.data.id);
        } else if (data.event == "del") {
            deleteModuleDialog(data.data.id);
        }
    });

    /**
     * 打开添加资源对话框
     * @param grade
     * @param parentId
     */
    function openAddModuleDialog(grade, parentId) {
        var title = "<h2>菜单管理 - 添加目录</h2>";
        var url = ctx + "/module/toAddModulePage?grade=" + grade + "&parentId=" + parentId;

        //iframe层
        layer.open({
            type: 2,
            title: title,
            shadeClose: true,
            shade: 0.8,
            area: ['550px', '500px'],
            content: url,
            maxmin: true,
        });
    }

    function openUpdateModuleDialog(moduleId) {
        var title = "<h3>菜单管理-修改资源</h3>";
        var url = ctx + "/module/toUpdateModulePage?moduleId=" + moduleId;

        layui.layer.open({
            type: 2,
            title: title,
            shadeClose: true,
            content: url,
            shade: 0.8,
            area: ['550px', '500px'],
            maxmin: true
        });
    }

    function deleteModuleDialog(moduleId) {
        layer.confirm('确认删除', {icon: 3, title: "菜单管理"}, function (index) {

            //关闭确认源
            layer.close(index);
            $.ajax({
                type: "post",
                url: ctx + "/module/delete",
                data: {
                    moduleId: moduleId,
                },
                success: function (result) {
                    if (result.code == 200) {
                        //成功
                        layer.msg("删除资源成功", {icon: 6});
                        tableIns.reload();
                    } else {
                        layer.msg("删除资源失败", {icon: 5});
                    }
                }
            });
        });
    }
});